﻿using Asp.Versioning;
using FlyingEye.DeviceMaintenanceServices;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace FlyingEye.Controllers
{
    [Route("api/device-ops-service/v{version:apiVersion}/maintenance-members")]
    [ApiVersion("1.0")]
    [ControllerName("MaintenanceMember")]
    [Authorize]
    public class MaintenanceMemberController : DefaultController
    {
        private readonly MaintenanceMemberService _maintenanceMemberService;

        public MaintenanceMemberController(MaintenanceMemberService maintenanceMemberService)
        {
            _maintenanceMemberService = maintenanceMemberService;
        }

        [HttpGet("{id}", Name = "GetByMaintenanceMemberId")]
        public async Task<ActionResult<MaintenanceMemberResponse>> GetAsync(Guid id)
        {
            return await GetInvokeAsync(() =>
            {
                return _maintenanceMemberService.GetAsync(id);
            });
        }

        [HttpPost]
        public async Task<ActionResult<MaintenanceMemberResponse>> InsertAsync([FromBody] CreateMaintenanceMemberRequest member)
        {
            return await InsertInvokeAsync(async () =>
            {
                return await _maintenanceMemberService.InsertAsync(member);
            }, "GetByMaintenanceMemberId", (result) => new { id = result.Id });
        }

        [HttpDelete("{id}")]
        public Task<NoContentResult> DeleteAsync(Guid id)
        {
            return DeleteInvokeAsync(async () =>
            {
                await _maintenanceMemberService.DeleteAsync(id);
            });
        }
    }
}
